/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.analyzers.filters;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;

/**
 * Filter to obtain the root of a word in English.
 * @author grupo JFTS
 *
 */
public class EnglishStemmerFilter extends TokenFilter{
	private EnglishStemmer stemmer = null;    
	private Set<Object> exclusions = null;
	private boolean ignoreCase = false;	

	
	public EnglishStemmerFilter(TokenStream in, boolean ignoreCase) {    
		super(in);    
		stemmer = new EnglishStemmer();
		this.ignoreCase = ignoreCase;
	}
	

	public EnglishStemmerFilter(TokenStream in, Set<Object> exclusions, boolean ignoreCase) {    
		super(in);    
		stemmer = new EnglishStemmer();
		this.exclusions = exclusions;
		this.ignoreCase = ignoreCase;
	}	
	

    public final Token next(final Token reusableToken) throws IOException {
    	assert reusableToken != null;
    	Token nextToken = input.next(reusableToken);
    	if (nextToken == null){
    		return null;
    	}

    	String term = nextToken.term();

    	if (exclusions == null || !_contains(term)) {
    		stemmer.setCurrent(term);
	    	stemmer.stem();    		
	    	String s = stemmer.getCurrent();
	    	if (ignoreCase){
		    	if ((s != null) && !s.equalsIgnoreCase(term)){
	    			nextToken.setTermBuffer(s);
	    		}
	    	}
	    	else{
		    	if ((s != null) && !s.equals(term)){
	    			nextToken.setTermBuffer(s);
	    		}	    		
	    	}
    	}
    	return nextToken;
    }    
    
    
    public void setStemmer(EnglishStemmer stemmer){    
        if ( stemmer != null ) {    
            this.stemmer = stemmer;    
        }    
    }
    
  
    private boolean _contains(String term){
    	Iterator<Object> it = exclusions.iterator();    	
    	boolean find = false;
    	
    	
    	while (it.hasNext() && !find){
    		String aux = (String) it.next();

    		if (ignoreCase){
	    		if (aux.equalsIgnoreCase(term)){
	    			find = true;
	    		}    			
    		}
    		else{
	    		if (aux.equals(term)){
	    			find = true;
	    		}
    		}
    	}
    	return find;
    }
}